package com.oig.sys.auth.controller;

import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.auth.dto.JobDto;
import com.oig.sys.auth.service.ITbJobService;
import com.oig.sys.auth.vo.JobVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "3-岗位管理")
@RestController
@RequestMapping("/job")
@Slf4j
public class TbJobController {

    private final ITbJobService jobService ;

    @Autowired
    public TbJobController(ITbJobService jobService){
        this.jobService = jobService ;
    }

    @PreAuthorize("@pms.hasPermission('/job/addJob')")
    @SysLog(value="添加岗位",apiUrl="/job/addJob")
    @Operation(summary = "31-添加岗位,可以同时添加岗位角色权限")
    @PostMapping("/addJob")
    public JobVo addJob(@RequestBody @Validated(GroupAdd.class) JobDto jobDto){
        log.info("addJob : {}",jobDto);
        return this.jobService.addJob(jobDto);
    }

    @PreAuthorize("@pms.hasPermission('/job/updateJob')")
    @SysLog(value="修改岗位",apiUrl="/job/updateJob")
    @Operation(summary="32-修改岗位")
    @PostMapping("/updateJob")
    public JobVo updateJob(@RequestBody @Validated(GroupEdit.class) JobDto jobDto){
        log.info("updateJob : {}",jobDto);
        return this.jobService.updateJob(jobDto);
    }

    @PreAuthorize("@pms.hasPermission('/job/cancelJob')")
    @SysLog(value="逻辑删除岗位",apiUrl="/job/cancelJob")
    @Operation(summary="33-逻辑删除岗位")
    @PostMapping("/cancelJob/{jobId}")
    public SysResponse<Integer> cancelJob(@PathVariable("jobId") Integer jobId){
        log.info("cancelJob : {}",jobId);
        this.jobService.cancelJob(jobId);
        return SysResponse.ok(jobId,"删除成功");
    }


    @PreAuthorize("@pms.hasPermission('/job/revertJob')")
    @SysLog(value="恢复岗位",apiUrl="/job/revertJob")
    @Operation(summary = "恢复岗位")
    @PostMapping(value = "/revertJob/{jobId}")
    public SysResponse<Integer> revertJob(@PathVariable("jobId") Integer jobId){
        log.info("revert job Id:{}",jobId);
        this.jobService.revertJob(jobId);
        return SysResponse.ok(jobId,"恢复成功");
    }

    @PreAuthorize("@pms.hasPermission('/job/delJob')")
    @SysLog(value="物理删除岗位",apiUrl="/job/delJob")
    @Operation(summary = "物理删除岗位,物理删除岗位及岗位员工关系")
    @PostMapping(value = "/delJob/{jobId}")
    public SysResponse<Integer> delJob(@PathVariable("jobId") Integer jobId){
        log.info("del job id={}", jobId) ;
        this.jobService.delJob(jobId);
        return SysResponse.ok(jobId,"物理删除成功");
    }

    //@PreAuthorize("@pms.hasPermission('/job/listAllJob')")
    @Operation(summary = "30-查询岗位,岗位管理,用户选岗位都用这个接口,不分页")
    @PostMapping(value = "/listAllJob")
    public List<JobVo> listAllJob(@RequestBody SysRequest<JobDto> jobRequest){
        log.info("list all job, {}", jobRequest);
        if (jobRequest.getParamDto()==null){
            jobRequest.setParamDto(new JobDto());
        }
        if (jobRequest.getParamDto().getTenantId()==null){
            log.debug("null tenantId");
            jobRequest.getParamDto().setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
        }
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            if (!jobRequest.getParamDto().getTenantId().equals(SecurityUtil.getDefineUserMain().getTenantId())){
                throw new BadRequestException("orgzId参数错误，不是租户或无租户权限");
            }
        }
        return this.jobService.listAllJob(jobRequest);
    }


    //@PreAuthorize("@pms.hasPermission('/job/info')")
    @Operation(summary = "34-查看岗位,查看详情，包含子表信息")
    @PostMapping(value = "/info/{jobId}")
    public JobVo info(@PathVariable("jobId") Integer jobId){
        log.info("info job id={}", jobId) ;
        return this.jobService.getJobInfo(jobId);
    }


}
